package com.smyh.UIM.host;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.smyh.UIM.HostFragment;
import com.smyh.UIM.R;
import com.smyh.UIM.device.DeviceUpdateFm;
import com.smyh.cache.UIM.device.DeviceUpdateFm_Cache;
import com.smyh.cache.UIM.host.HostUpdateFm_Cache;
import com.smyh.protcol.host.ProtocolCommMethod;
import com.smyh.thread.UpdateHostProgramThread;
import com.smyh.util.TxtUtil;

public class HostUpdateFm extends Fragment {

    /**
     * 组号单选框
     * 组号组
     */
    RadioButton group1Btn,group2Btn,group3Btn;
    RadioGroup groupRadioGroup;
    /**
     * 相别单选框
     * 相别组
     */
    RadioButton phaseABtn,phaseBBtn,phaseCBtn,phaseMBtn;
    RadioGroup phaseRadioGroup;

    /**
     * 文件上传按钮，续传按钮，指令升级按钮
     */
    Button updateBtn,contUpdateBtn,cmdUpdateBtn;

    TextView progressHostLabel,updataFileName;

    static Handler handler;
    /**
     * 文件资源地址
     */
    Uri uri;

    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_host_update, container, false);
        initView(view);
        initEvent();
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {

                switch (msg.what){
                    case 1:{

                        String progressStr = (String) msg.getData().get("1");
                        progressHostLabel.setText(progressStr);
                    }
                }
                super.handleMessage(msg);
            }
        };
        return view;
    }

    public static void freshHostUpdateUI(Message message){
        handler.sendMessage(message);
    }

    private void initEvent() {
        group1Btn.setOnCheckedChangeListener( radioCheckList);
        group2Btn.setOnCheckedChangeListener( radioCheckList);
        group3Btn.setOnCheckedChangeListener( radioCheckList);

        phaseABtn.setOnCheckedChangeListener( radioCheckList);
        phaseBBtn.setOnCheckedChangeListener( radioCheckList);
        phaseCBtn.setOnCheckedChangeListener( radioCheckList);
        phaseMBtn.setOnCheckedChangeListener( radioCheckList);

        updateBtn.setOnClickListener(btnOnClickList);
        contUpdateBtn.setOnClickListener(btnOnClickList);
        cmdUpdateBtn.setOnClickListener(btnOnClickList);
    }

    private void initView(View view) {
        DeviceUpdateFm_Cache.updateType = 0;
        group1Btn = (RadioButton) view.findViewById(R.id.group1Btn);
        group2Btn = (RadioButton) view.findViewById(R.id.group2Btn);
        group3Btn = (RadioButton) view.findViewById(R.id.group3Btn);

        groupRadioGroup  = (RadioGroup) view.findViewById(R.id.groupRadioGroup);

        phaseABtn = (RadioButton) view.findViewById(R.id.phaseABtn);
        phaseBBtn = (RadioButton) view.findViewById(R.id.phaseBBtn);
        phaseCBtn = (RadioButton) view.findViewById(R.id.phaseCBtn);
        phaseMBtn = (RadioButton) view.findViewById(R.id.phaseMBtn);

        phaseRadioGroup  = (RadioGroup) view.findViewById(R.id.phaseRadioGroup);

        updateBtn = (Button) view.findViewById(R.id.hostUpdateFileBtn);
        contUpdateBtn = (Button) view.findViewById(R.id.continUpdateBtn);
        cmdUpdateBtn = (Button) view.findViewById(R.id.cmdUpdateBtn);

        progressHostLabel = (TextView) view.findViewById(R.id.progressHostLabel);
        updataFileName = (TextView) view.findViewById(R.id.updataFileName);
    }

    private CompoundButton.OnCheckedChangeListener radioCheckList = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if(isChecked){
                switch (buttonView.getId()){
                    case R.id.group1Btn:{
                        HostUpdateFm_Cache.setSelectedHostGroup((byte) 1);
                        break;
                    }
                    case R.id.group2Btn:{
                        HostUpdateFm_Cache.setSelectedHostGroup((byte) 2);
                        break;
                    }
                    case R.id.group3Btn:{
                        HostUpdateFm_Cache.setSelectedHostGroup((byte) 3);
                        break;
                    }
//====================================================
                    case R.id.phaseABtn:{
                        HostUpdateFm_Cache.setSelectedHostPhase((byte) 1);
                        break;
                    }
                    case R.id.phaseBBtn:{
                        HostUpdateFm_Cache.setSelectedHostPhase((byte) 2);
                        break;
                    }
                    case R.id.phaseCBtn:{
                        HostUpdateFm_Cache.setSelectedHostPhase((byte) 3);
                        break;
                    }
                    case R.id.phaseMBtn:{
                        HostUpdateFm_Cache.setSelectedHostPhase((byte) 0);
                        break;
                    }
                }
            }
        }
    };

    View.OnClickListener btnOnClickList = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()){
                case R.id.hostUpdateFileBtn:{
                    Intent it2 = new Intent(Intent.ACTION_GET_CONTENT);
                    it2.setType("*/*");
                    it2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                    startActivityForResult(Intent.createChooser(it2, "选个文件"),1);
                    break;
                }
                case R.id.continUpdateBtn:{
                    String realPath = DeviceUpdateFm.getRealFilePath(getContext(), uri);
                    /*
                     *主机升级，文件续传操作：
                     */
                    TxtUtil.updateProgramPackForHost(realPath, UpdateHostProgramThread.prev);
                    break;
                }
                case R.id.cmdUpdateBtn:{
                    if (HostFragment.connection_btn.isChecked()) {
                        // 设备透传：或者处于串口数据运行模式：
                        ProtocolCommMethod.sendHostUpdateProgramCmd(HostUpdateFm_Cache.selectedHostDeviceIndex);
                    } else {
                        Toast.makeText(getContext(),"请连接设备，使数据透传！",Toast.LENGTH_SHORT).show();
                    }
                    break;
                }
            }
        }
    };
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 1 && resultCode == Activity.RESULT_OK) {

            uri = data.getData();
            String realPath = DeviceUpdateFm.getRealFilePath(getContext(), uri);
            updataFileName.setText(realPath);

            TxtUtil.updateProgramPackForHost(realPath, 0);
        }
    }
}
